package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.DanmoPrintingFavorite;
import com.muyu.danmo.domain.DanmoPrintingLikes;
import com.muyu.danmo.domain.DanmoPrintingMessage;
import com.muyu.danmo.mapper.DanmoPrintingFavoriteMapper;
import com.muyu.danmo.mapper.DanmoPrintingMessageMapper;
import com.muyu.danmo.service.IDanmoPrintingFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 印刷出版收藏Service业务层处理
 * 
 * @author kunzong
 * @date 2024-12-05
 */
@Service
public class DanmoPrintingFavoriteServiceImpl implements IDanmoPrintingFavoriteService
{
    @Autowired
    private DanmoPrintingFavoriteMapper danmoPrintingFavoriteMapper;

    @Autowired
    private DanmoPrintingMessageMapper danmoPrintingMessageMapper;

    /**
     * 查询印刷出版收藏
     * 
     * @param id 印刷出版收藏主键
     * @return 印刷出版收藏
     */
    @Override
    public DanmoPrintingFavorite selectDanmoPrintingFavoriteById(Long id)
    {
        return danmoPrintingFavoriteMapper.selectDanmoPrintingFavoriteById(id);
    }

    /**
     * 查询印刷出版收藏列表
     * 
     * @param userId 用户ID
     * @return 印刷出版收藏
     */
    @Override
    public List<DanmoPrintingMessage> selectDanmoPrintingFavoriteList(Long userId )
    {
        return danmoPrintingMessageMapper.selectPrintingMessageFavoriteList(userId);
    }


    /**
     * 修改印刷出版收藏
     * 
     * @param danmoPrintingFavorite 印刷出版收藏
     * @return 结果
     */
    @Override
    public int updateDanmoPrintingFavorite(DanmoPrintingFavorite danmoPrintingFavorite)
    {
        danmoPrintingFavorite.setUpdateTime(DateUtils.getNowDate());
        return danmoPrintingFavoriteMapper.updateDanmoPrintingFavorite(danmoPrintingFavorite);
    }

    /**
     * 批量删除印刷出版收藏
     * 
     * @param ids 需要删除的印刷出版收藏主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoPrintingFavoriteByIds(Long userId,Long[] ids)
    {
        for (Long id : ids) {
            if (!danmoPrintingFavoriteMapper.existsByUserIdAndPrintingClassId(userId, id)) {
                return Result.error("取消失败");
            }
        }
        danmoPrintingFavoriteMapper.deleteDanmoPrintingFavoriteByIds(ids, userId);
        return Result.success(1,"取消成功");
    }

    /**
     * 删除印刷出版收藏信息
     * 
     * @param id 印刷出版收藏主键
     * @return 结果
     */
    @Override
    public int deleteDanmoPrintingFavoriteById(Long id)
    {
        return danmoPrintingFavoriteMapper.deleteDanmoPrintingFavoriteById(id);
    }

    /**
     * 添加收藏
     * @param userId
     * @param id
     * @return
     */
    @Override
    public Result insertDanmoPrintingFavorite(Long userId, Long id) {
        if (danmoPrintingFavoriteMapper.existsByUserIdAndPrintingClassId(userId, id)) {
            return Result.success( 1,"用户已收藏");
        }
        DanmoPrintingFavorite danmoPrintingFavorite = new DanmoPrintingFavorite();
        danmoPrintingFavorite.setUserId(userId);
        danmoPrintingFavorite.setProductId(id);
        danmoPrintingFavorite.setCreateBy(String.valueOf(userId));
        danmoPrintingFavorite.setCreateTime(DateUtils.getNowDate());
        danmoPrintingFavoriteMapper.insertDanmoPrintingFavorite(danmoPrintingFavorite);
        return Result.success(1,"收藏成功");
    }
}
